<?php
// +----------------------------------------------------------------------
// | Quotes [ 只为给用户更好的体验]**[我知道发出来有人会盗用,但请您留版权]
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 零度  <2109877665@qq.com>          盗用不留版权,你就不配拿去!
// +----------------------------------------------------------------------
// | Date: 2019年08月20日
// +----------------------------------------------------------------------

include("../Core/Common.php");
$act=$_GET['act'];
if($islogin_admin==1 or $act=='Login'){}else exit("<script language='javascript'>window.location.href='./Login.php';</script>");
if($act=='Login'){//登录后台
	$admin_user=daddslashes($_POST['admin_user']);
	$admin_pass=daddslashes($_POST['admin_pass']);
		if(!$admin_user or !$admin_pass){
			$result=array("code"=>-1,"msg"=>"所有参数不能为空");
		}elseif($admin_user==$conf['admin_user'] && $admin_pass==$conf['admin_pass']) {
			$session=md5($admin_user.$admin_pass.$password_hash);
			$token=authcode("{$user}\t{$session}", 'ENCODE', $conf['KEY']);
			setcookie("admin_token", $token, time() + 604800);
			$result=array("code"=>1,"msg"=>"登录成功");
		}elseif($admin_pass != $conf['admin_pass']) {
			$_SESSION['pass_error']++;
			$result=array("code"=>-1,"msg"=>"登录失败");
		}
}elseif($act=='Add_daili'){//添加授权商
    $qq= $_POST['qq'];
	$nums= $_POST['nums'];
	$sds="INSERT INTO `pay_daili` (`nickname`, `qq_id`, `qq`, `nums`,`addtime`) VALUES ('{$nickname}','','{$qq}','{$nums}','{$date}')";
	if(!$qq or !$nums){
			$result=array("code"=>-1,"msg"=>"所有参数不能为空");
		}elseif($DB->exec($sds)){	
	/*
		$email=$qq.'@qq.com';
		$sub = 'INTL二维码Pay - 成为授权商提醒';
		$msg = '恭喜您成为：INTL二维码Pay - 授权商 ,温馨提示:请勿低价授权,泛滥授权,否则回收权限!';
		send_mail($email, $sub, $msg);
	*/
		$result=array("code"=>1,"msg"=>"添加授权商成功");
	}else{
		$result=array("code"=>-1,"msg"=>"添加授权商失败");
	}
}elseif($act=='Edit_daili'){	//修改授权商
    $id= $_POST['id'];
    $qq= $_POST['qq'];
	$nums= $_POST['nums'];
	$is=$DB->query("SELECT * FROM `pay_daili` WHERE `id`='{$id}' limit 1")->fetch();
	if(!$is){
		$result=array("code"=>-2,"msg"=>"此用户记录不存在");
	}else{
		$sqs=$DB->exec("update `pay_daili` set `nums` ='{$nums}' where id='{$id}'");
		if($sqs){
			$result=array("code"=>1,"msg"=>"修改授权商成功");
		}else{
			$result=array("code"=>-1,"msg"=>"修改授权商失败");
		}
	}
}elseif($act=='Edit_user'){	//修改支付额度
	$id= $_POST['id'];
	$money= $_POST['money'];
	$pc_check = $_POST['pc_check'];
	$alipay_up_outtime = $_POST['alipay_up_outtime'].date(" H:i:s");
	$is=$DB->query("SELECT * FROM `pay_cloud` WHERE `id`='{$id}' limit 1")->fetch();
	if(!$is){
		$result=array("code"=>-2,"msg"=>"此用户记录不存在");
	}else{
		if($_POST['alipay_up_outtime'])$sqls =",`alipay_up_outtime`='{$alipay_up_outtime}'";
		$sqs=$DB->exec("update `pay_cloud` set `money`='{$money}' , `pc_check`='{$pc_check}'{$sqls} where id='{$id}'");
		if($sqs){
			$result=array("code"=>1,"msg"=>"修改用户成功");
		}else{
			$result=array("code"=>-1,"msg"=>"修改用户失败");
		}
	}
}elseif($act=='Status_daili'){//禁封/解封授权商
	$id=daddslashes($_POST['id']);
	$is=$DB->query("SELECT * FROM `pay_daili` WHERE `id`='{$id}' limit 1")->fetch();
		if(!$is){
			$result=array("code"=>-2,"msg"=>"此授权商记录不存在");
		}else{
			if($is['status']==1){
				$msg_a = '禁封授权商成功';
				$msg_b = '禁封授权商失败';
				$sqs=$DB->exec("update `pay_daili` set `status`='0' where id='{$id}'");
			}else{
				$msg_a = '解封授权商成功';
				$msg_b = '解封授权商失败';
				$sqs=$DB->exec("update `pay_daili` set `status`='1' where id='{$id}'");
			}
			if($sqs){
				$result=array("code"=>1,"msg"=>$msg_a);
			}else{
				$result=array("code"=>-1,"msg"=>$msg_b);
			}
		}
}elseif($act=='Add_check'){//添加授权	
	$url= $_POST['url'];
	$qq= $_POST['qq'];
	$money= $_POST['money'];
	$beizhu= $_POST['beizhu'];
    $pid= $_POST['pid'];
	$key= $_POST['key'];
	$is=$DB->query("SELECT * FROM `pay_check` WHERE `qq`='{$qq}' limit 1")->fetch();
	if($is){
		$authcode=$is['authcode'];
	}else{
		$authcode=md5(mt_rand(1000000000,9999999999).$qq);
	}
	$sds=$DB->exec("INSERT INTO `pay_check` (`daili_id`,`url`,`qq`,`beizhu`,`authcode`,`addtime`) VALUES ('admin','{$url}','{$qq}','{$beizhu}','{$authcode}','{$date}')");
	if($sds){
		$is=$DB->query("SELECT * FROM `pay_cloud` WHERE `qq`='{$qq}' limit 1")->fetch();
		if(!$is){
			$pid= '1'.mt_rand(10000000,99999999);
			$key= '1'.mt_rand(10000000,99999999);
			$nickname = match_chinese(getQQNick($qq));;
			$money = 1000;
			$sds=$DB->exec("INSERT INTO `pay_cloud` (`nickname`,`qq_id`,`qq`,`pid`,`key`,`money`) VALUES ('{$nickname}','','{$qq}','{$pid}','{$key}','{$money}')");
		}
		if($sds){
			$result=array("code"=>1,"msg"=>"添加授权成功");
		}else{
			$result=array("code"=>1,"msg"=>"添加授权成功,但生成云端账号密码失败");
		}
		/*
		$email=$qq.'@qq.com';
		$sub = 'INTL二维码Pay - 成为授权用户提醒';
		$msg = '恭喜您成为：INTL二维码Pay - 授权用户('.$url.') ,温馨提示:请勿泛滥授权,否则回收权限,感谢您的支持!';
		send_mail($email, $sub, $msg);
		*/
	}else{
		$result=array("code"=>-1,"msg"=>"添加授权失败");
	}
}elseif($act=='Edit_check'){	//修改授权
	$id= $_POST['id'];
	$url= $_POST['url'];
	$qq= $_POST['qq'];
	$ip= $_POST['ip'];
	$money= $_POST['money'];
	$beizhu= $_POST['beizhu'];
    $pid= $_POST['pid'];
	$key= $_POST['key'];
	$is=$DB->query("SELECT * FROM `pay_check` WHERE `id`='{$id}' limit 1")->fetch();
	if(!$is){
		$result=array("code"=>-2,"msg"=>"此授权记录不存在");
	}else{
		$sqs=$DB->exec("update `pay_check` set `url`='{$url}',`qq`='{$qq}',`beizhu` ='{$beizhu}' where id='{$id}'");
		if($sqs){
			$result=array("code"=>1,"msg"=>"修改授权成功");
		}else{
			$result=array("code"=>-1,"msg"=>"修改授权失败");
		}
	}
}elseif($act=='Status_check'){//禁封/解封授权
	$id=daddslashes($_POST['id']);
	$is=$DB->query("SELECT * FROM `pay_check` WHERE `id`='{$id}' limit 1")->fetch();
		if(!$is){
			$result=array("code"=>-2,"msg"=>"此授权记录不存在");
		}else{
			if($is['status']==1){
				$msg_a = '禁封授权成功';
				$msg_b = '禁封授权失败';
				$sqs=$DB->exec("update `pay_check` set `status`='0' where qq='{$is['qq']}'");
			}else{
				$msg_a = '解封授权成功';
				$msg_b = '解封授权失败';
				$DB->exec("DELETE FROM `pay_cron_url` WHERE `url`='{$is['url']}'");
				$sqs=$DB->exec("update `pay_check` set `status`='1' where qq='{$is['qq']}'");
			}
			if($sqs){
				$result=array("code"=>1,"msg"=>$msg_a);
			}else{
				$result=array("code"=>-1,"msg"=>$msg_b);
			}
		}
}
elseif($act=='saveNotice'){	//修改后台配置信息
	foreach($_POST as $k=>$v){
		saveSetting($k, $v);
	}
	$result=array("code"=>1,"msg"=>"修改成功!");
	$ad=$CACHE->clear();
	
}elseif($act=='Set'){	//修改后台配置信息
	foreach($_POST as $k=>$v){
		saveSetting($k, $v);
	}
	//云端配置信息
	$Instant_url=daddslashes($_POST['Instant_url']);
	$Instant_pid=daddslashes($_POST['Instant_pid']);
	$Instant_key=daddslashes($_POST['Instant_key']);
		if(!$Instant_url or !$Instant_pid or !$Instant_key){
			$result=array("code"=>-1,"msg"=>"修改失败:所有内容不能留空");
		}else{
			$Instant_Api=new Instant_Api($Instant_url,$Instant_pid,$Instant_key);
			$Query = $Instant_Api->Query();
			if($Query['code']==1 && $Instant_pid && $Instant_key){
				saveSetting('Instant_url',$_POST['Instant_url']);
				saveSetting('Instant_pid',$_POST['Instant_pid']);
				saveSetting('Instant_key',$_POST['Instant_key']);	
				$result=array("code"=>1,"msg"=>"修改成功!");
			}elseif($Instant_pid && $Instant_key){
				$result=array("code"=>-1,"msg"=>"修改失败,云端API_Pid或Key错误,无法保存配置,请重试！[".$Query['msg']."]");
			}
		}
	$ad=$CACHE->clear();
}elseif($act=='Update'){	//在线更新配置
    saveSetting('ver',$_POST['ver']);
    saveSetting('vers',$_POST['vers']);
    saveSetting('uplog',$_POST['uplog']);
    saveSetting('content',$_POST['content']);
    saveSetting('is_up',$_POST['is_up']);
	$ad=$CACHE->clear();
	if($ad)$result=array("code"=>1,"msg"=>"成功修改");
	else $result=array("code"=>-1,"msg"=>"修改失败");
}else{
	$result=array("code"=>-9,"msg"=>"参数错误");
}
if($result)
	exit(json_encode($result));
else
	exit($data);
?>